package com.frxuexi.shop2307.fragment

import android.content.Context
import android.util.Log
import android.widget.CursorAdapter
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.frxuexi.cart.adapter.CouponAdapter
import com.frxuexi.lib_base.entity.ExplosiveMoney
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.shop2307.adapter.HomeMenuAdapter
import com.frxuexi.shop2307.adapter.ShopAdapter
import com.frxuexi.shop2307.databinding.FragmentHomeBinding
import com.frxuexi.shop2307.shop.ShopViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader
import com.youth.banner.loader.ImageLoaderInterface
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding, ShopViewModel>() {
    val homeMenuAdapter by lazy { HomeMenuAdapter() }
    val shopAdapter by lazy { ShopAdapter() }
    val couponAdapter by lazy { CouponAdapter() }
    val menus= mutableListOf<ExplosiveMoney>()
    var page=1
    var total=0

    override fun initData() {
        vm.shopError.observe(this){
            ToastUtils.showLong(it)
        }
        vm.homeData.observe(this){
            Log.i("lr",it.toString())
            binding.homeTab.apply {
                removeAllTabs()
                menus.clear()
                menus.addAll(it.explosiveMoney)
                it.explosiveMoney.forEach {
                    addTab(newTab().setText(it.name))
                }
            }

            binding.homeBanner.apply {
                setImages(it.explosiveMoney.map { it.pic })
                setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        imageView!!.scaleType=ImageView.ScaleType.FIT_XY
                        GlideUtils.getRoundImage(imageView!!,path.toString())
                    }
                })
                setDelayTime(3000)
                start()
            }

            homeMenuAdapter.apply {
                data.clear()
                addData(it.menus)
                notifyDataSetChanged()
            }

        }

        vm.productTypeData.observe(this){
            total=it.totalPage
            shopAdapter.apply {
                data.clear()
                addData(it.list)
                notifyDataSetChanged()
            }
        }

        vm.couponsData.observe(this){
            couponAdapter.apply {
                data.clear()
                addData(it)
                notifyDataSetChanged()
            }
        }

        vm.receiveCouponData.observe(this){
            vm.getCoupons()
        }

    }

    override fun initView() {
        vm.getHomes()
        vm.getCoupons()

        binding.homeSv.setOnClickListener {
            ArouterManager.nav(requireContext(),"/app/SearchActivity")
        }

        binding.homeMenuRv.apply {
            adapter=homeMenuAdapter
            layoutManager=GridLayoutManager(requireContext(),5)
        }

        homeMenuAdapter.setOnItemClickListener { adapter, view, position ->
            when(position){
                2->ArouterManager.nav(requireContext(),"/app/SignActivity")
                3->ArouterManager.nav(requireContext(),"/app/ShopLoveActivity")
                4->ArouterManager.nav(requireContext(),"/app/PinActivity")
                5->ArouterManager.nav(requireContext(),"/app/KillActivity")

                7->ArouterManager.nav(requireContext(),"/cart/AddressListActivity")
                8->ArouterManager.nav(requireContext(),"/cart/OrderActivity")

            }
        }

        binding.homeRv.apply {
            adapter=shopAdapter
            layoutManager=StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
        }
        vm.getProductsByType(4,page)

        binding.homeTab.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                vm.getProductsByType(menus.get(tab?.position!!).type.toInt(),1)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })

        shopAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(requireContext(),"/app/ShopDetailActivity",shopAdapter.data.get(position).id)
        }

        binding.homeSmart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                vm.getProductsByType(4,page)
                binding.homeSmart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if(page<total) {
                    page++
                    vm.getProductsByType(4, page)
                }
                binding.homeSmart.finishLoadMore()
            }
        })

        binding.homeHbRv.apply {
            adapter=couponAdapter
            layoutManager=LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
        }

        couponAdapter.setOnItemClickListener { adapter, view, position ->
            vm.receiveCoupon(mapOf("couponId" to couponAdapter.data.get(position).id))
        }
    }

}